<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title> 
    <link rel="stylesheet" href="CSS/common.css">
    <link rel="stylesheet" href="CSS/blog.list.css">
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="imger/2.jpg" alt="这是一个logo图标">
        <span class="title">我的博客系统</span>
        <!-- 用来空出中间的位置 -->
        <span class="spacer"></span>
        <a href="blong.html">主页</a>
        <a href="blong_2.html">写博客</a>
        <a href="logout">注销</a>
       
    </div>


     <!-- 中间板块 -->
     <div class="container">
        <!-- 左侧个人信息 -->
        <div class="left">
             <!-- 用户详情 -->
            <div class="card">
                <img src="imger/2.jpg"class="avtar" alt="用户头像">
                <h3>Sunlightʊə</h3>
                <a href="https://gitee.com/fiber-cloud/projects?sort=&scope=&state=private">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="count"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        
        <!-- 右侧详细内容 -->
        <div class="right"></div>
    </div>

    <script src="./js/jquery.min.js"></script>
    <script>
        //在页面加载时向服务器发起请求获取数据
        function getBlogs(){
            $.ajax({
                type:'get',
                url:'blog',
                success:function(body){
                    //body是json字符串 此处已经被jquery自动解析成js对象数据，直接for循环遍历
                    let containerRight = document.querySelector('.right');
                    for(let blog of body){
                        //构造页面内容
                        let blogDiv=document.createElement('div');
                        blogDiv.className='blog';
                        //构造标题
                        let titleDiv=document.createElement('div');
                        titleDiv.className='title';
                        titleDiv.innerHTML=blog.title;
                        blogDiv.appendChild(titleDiv);
                        //构造发布时间
                        let dateDiv=document.createElement('div');
                        dateDiv.className='date';
                        dateDiv.innerHTML=blog.postTime;
                        blogDiv.appendChild(dateDiv);
                        //构造正文（这里要截断）
                        let descDiv=document.createElement('div');
                        descDiv.className='desc';
                        descDiv.innerHTML=blog.content;
                        blogDiv.appendChild(descDiv);
                        //构造按钮
                        let a=document.createElement('a');
                        a.innerHTML='查看全文&gt;&gt;';
                        //期望点击能够跳转到博客详情页
                        a.href='blong_3.html?blogId='+blog.blogId;
                        blogDiv.appendChild(a);
                        //把blogDiv加到父元素中
                        containerRight.appendChild(blogDiv);
                    }
                }
            });
        }
        getBlogs();

        function chekLogin(){
            $.ajax({
                type:'get',
                url:'login',
                
                success:function(body){
                    if(body.userId && body.userId>0){
                        console.log("当前用户已登录");

                        let h3=document.querySelector('.left .card h3');
                        h3.innerHTML=body.username;
                    }else{
                        location.assign('login.html');
                    }
                }

            });
        }
        chekLogin();


        function countBlog(){
            $.ajax({
                url:'user',
                type:'get',
                success:function(body){
                    let count=document.querySelector('#count');
                    count.innerHTML=body;
                    console.log('body');
                }
            });
        }
        countBlog();


    </script>
</body> 
</html>